/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.pdx.ncn.background;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.rhok.pdx.ncn.application.ApplicationContext;
import org.rhok.pdx.ncn.datatypes.LocationData;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

/**
 * Listener to track the phone's current GPS location and update a text view with that information
 * as it changes.
 * @author pfritzsche
 */
public class TrackingLocationListener implements LocationListener {
	/**
	 * Context interface specific to this class. Only allows access to the methods the class needs
	 * access to, in order to prevent accidental changes to shared data. Documentation provided at
	 * {@link ApplicationContext}.
	 * @author pfritzsche
	 */
	public interface LocationListenerContext {
		public ConcurrentLinkedQueue<LocationData> locs();
		public int signalStrength();
		public int wifiStrength();
	}

	private final EditText textField;
	private final LocationListenerContext context;
	
	/**
	 * Public constructor.
	 * @param context Shared application context, to be used for data sharing between classes
	 */
	public TrackingLocationListener(LocationListenerContext context, EditText textField) {
		this.context = context;
		this.textField = textField;
	}
	
	/**
	 * Called when the phone's GPS location has changed. Updates the GPS text view with the current
	 * latitude and longitude, adds the location to the queue of shared locations for the map
	 * overlay system, and adds the information to the database for permanent storage.
	 * @param location object containing all new location information
	 */
	public void onLocationChanged(Location location) {
		LocationData data = new LocationData(location.getLatitude(), location.getLongitude(),
				context.signalStrength(), context.wifiStrength());
		Log.d("TrackingLocationListener", "Data: " + data.toString());
		context.locs().offer(data);
		if (textField != null) {
			textField.setText(location.getLatitude() + ", " + location.getLongitude());
		}
	}

	/**
	 * Required method to implement the interface. We don't care about what happens when the
	 * provider has been disabled, so this is left doing nothing.
	 */
	public void onProviderDisabled(String provider) { }
	
	/**
	 * Required method to implement the interface. We don't care about what happens when the
	 * provider has been enabled, so this is left doing nothing.
	 */
	public void onProviderEnabled(String provider) { }
	
	/**
	 * Required method to implement the interface. We don't care about what happens when any
	 * non-location statuses have changed, so this is left doing nothing.
	 */
	public void onStatusChanged(String provider, int status, Bundle extras) { }

}
